﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ParallelDemo
{
    class ParallelFor
    {
        internal void ParaFor()
        {
            ParallelLoopResult result = Parallel.For(0, 10, new ParallelOptions() { MaxDegreeOfParallelism = 10 }, (i, ParallelLoopState) =>
            {
                mothedfor(i, ParallelLoopState);
            });

            Console.WriteLine("是否完成:{0}", result.IsCompleted);
            Console.WriteLine("最低迭代:{0}", result.LowestBreakIteration);
            Console.ReadKey();
        }

        private void mothedfor(int i, ParallelLoopState state)
        {
            Console.WriteLine("迭代次数：{0},任务ID:{1},线程ID:{2}", i, Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
            Thread.Sleep(10);
            if (i == 10)
            {
                state.Break();
            }
        }
    }
}
